package controller;

import java.awt.Component;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import model.Column;
import model.DataTable;
import view.Factory;
import view.Visualization;


/**
 * The controller accepts the formatted data from the model package as a
 * parameter. When instantiated, Controller prompts the user to make selections
 * about how they wish to visualize the data. The visualization of the data is
 * then return using the getVisualization() method.
 * 
 * @author Grant Oakley
 */
public class Controller {

    private static final String VISUALIZATION_KEY = "Visualization";
    private static final String X_AXIS_KEY = "xAxis";
    private static final String Y_AXIS_KEY = "yAxis";

    private Visualization myVisualization;

    /**
     * Creates a new instance of controller for the DataTable provided. This
     * class prompts the user to select what type of visualization they would
     * like to plot, as well as what data vectors from the DataTable they would
     * like to graph on the axes.
     * 
     * @param frame parent frame
     * @param title title for the visualization
     * @param language language in which to display the dialog box
     * @param data the data table that is to be visualized
     */
    public Controller (JFrame frame, String title, String language, DataTable data) {
        MultiFieldJOptionPane<String> multiOptionPane = getUserInput(frame, data);

        makeVisualization(title, data, multiOptionPane);
    }

    private MultiFieldJOptionPane<String> getUserInput (JFrame frame, DataTable data) {
        MultiFieldJOptionPane<String> multiOptionPane =
                new MultiFieldJOptionPane<String>(frame, "Select Data to Plot");
        JComboBox availableVisualizationsComboBox =
                new JComboBox(Factory.getAvailableVisualizations());
        JComboBox columnNamesComboBoxX = new JComboBox(data.getColumnNames().toArray());
        JComboBox columnNamesComboBoxY = new JComboBox(data.getColumnNames().toArray());
        multiOptionPane.addField(VISUALIZATION_KEY, "Select visualization type",
                                 availableVisualizationsComboBox);
        multiOptionPane.addField(X_AXIS_KEY, "X Axis: ", columnNamesComboBoxX);
        multiOptionPane.addField(Y_AXIS_KEY, "Y Axis: ", columnNamesComboBoxY);

        multiOptionPane.display();
        return multiOptionPane;
    }

    private void makeVisualization (String title, DataTable data,
                                    MultiFieldJOptionPane<String> multiOptionPane) {
        myVisualization = Factory.makeView(multiOptionPane.getResult(VISUALIZATION_KEY));
        String xData = multiOptionPane.getResult(X_AXIS_KEY);
        String yData = multiOptionPane.getResult(Y_AXIS_KEY);
        Column<?> xColumn = data.getColumn(xData);
        if (myVisualization.isCorrectNumberOfDataVectors(2)) {
            Column<?> yColumn = data.getColumn(yData);
            myVisualization.addDataVectors(xColumn.getContents(), yColumn.getContents());
            myVisualization.setAxisLabel(Visualization.Y_AXIS, yData);
        }
        else {
            myVisualization.addDataVectors(xColumn.getContents());
        }
        myVisualization.setAxisLabel(Visualization.X_AXIS, xData);
        myVisualization.setTitle(title);
    }

    /**
     * Returns the visualization created by controller with the user input.
     * 
     * @return the created visualization as a java.awt.Component
     */
    public Component getVisualization () {
        return myVisualization;
    }

}
